package cn.kivensoft.xlsx.helper;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.kivensoft.xlsx.reader.Row;
import cn.kivensoft.xlsx.reader.Sheet;
import cn.kivensoft.xlsx.reader.Workbook;

/*
		File f = new File("z:\\测试.xlsx");
		InputStream input = new FileInputStream(f);
		XlsxImport reader = new XlsxImport(input, (int)f.length(), headers);
		input.close();
		while (reader.nextRow()) {
			System.out.printf("%s, %d, %tF %<tT, %tF, %.2f\n",
					reader.cellAsString(0), reader.cellAsInteger(1),
					reader.cellAsDateTime(2), reader.cellAsDate(3),
					reader.cellAsDecimal(4));
		}
 */

public class XlsxImport {
	protected Workbook _workbook;
	protected Sheet _sheet;
	protected Row _currentRow;
	protected int _currentRowIndex;

	public Workbook getWorkbook() { return _workbook; }
	public Sheet getSheet() { return _sheet; }
	public Row getCurrentRow() { return _currentRow; }
	public int getCurrentRowIndex() { return _currentRowIndex; }
	
	public XlsxImport(String fileName, String[] headers) throws Exception {
		_workbook = new Workbook(fileName, null);
		findHeaders(headers);
	}

	public XlsxImport(InputStream input, int size, String[] headers) throws Exception {
		_workbook = new Workbook(input, size, null);
		findHeaders(headers);
	}

	private void findHeaders(String[] headers) throws Exception {
		// 校验工作表
		List<Sheet> sheets = _workbook.getSheets();
		int sheetsCount = sheets.size();
		if (sheetsCount == 0)
			throw new Exception("未找到工作表！");
		if (sheetsCount > 1)
			throw new Exception("工作表数量大于1个！");
		_sheet = sheets.get(0);
		_currentRowIndex = -1;

		if (headers == null) return;

		int headersLen = headers.length;
		while (nextRow()) {
			Row r = _currentRow;
			boolean matched = true;
			for (int i = 0; i < headersLen; i++) {
				String h = headers[i];
				if (h != null && !h.equals(r.cellAsString(i))) {
					matched = false;
					break;
				}
			}
			if (matched) return;
		}

		//找不到指定的列头，抛出异常
		StringBuilder sb = new StringBuilder(512);
		sb.append("未找到指定的列头[");
		boolean _first = true;
		for (String item : headers) {
			if (_first) _first = false;
			else sb.append(',');
			sb.append(item);
		}
		sb.append("]!");
		throw new Exception(sb.toString());
	}

	public boolean nextRow() {
		if (++_currentRowIndex >= _sheet.getRows().size()) return false;
		_currentRow = _sheet.getRows().get(_currentRowIndex);
		return _currentRow == null ? false : true;
	}

	public boolean hasMore() {
		return _currentRowIndex + 1 >= _sheet.getRows().size() ? false : true;
	}

	public int getRowCount() {
		return _sheet.getRows().size();
	}

	public int getCurrentColumnCount() {
		return _currentRow == null ? 0 : _currentRow.getCells().size();
	}

	public String cellAsString(int index) {
		return _currentRow == null ? null : _currentRow.cellAsString(index);
	}

	public Integer cellAsInteger(int index) {
		return _currentRow == null ? null : _currentRow.cellAsInteger(index);
	}

	public Double cellAsDouble(int index) {
		return _currentRow == null ? null : _currentRow.cellAsDouble(index);
	}

	public BigDecimal cellAsDecimal(int index) {
		return _currentRow == null ? null : _currentRow.cellAsDecimal(index);
	}

	public Date cellAsDate(int index) {
		return _currentRow == null ? null : _currentRow.cellAsDate(index);
	}

	public Date cellAsTime(int index) {
		return _currentRow == null ? null : _currentRow.cellAsTime(index);
	}

	public Date cellAsDateTime(int index) {
		return _currentRow == null ? null : _currentRow.cellAsDateTime(index);
	}

}
